package com.example.springsecurity.service.Impl.Security;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springsecurity.Mapper.UserMapper;
import com.example.springsecurity.common.Result;
import com.example.springsecurity.common.ServiceException;
import com.example.springsecurity.entity.LoginUser;
import com.example.springsecurity.entity.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        User u=userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getName,name));
        if(Objects.isNull(u)){
            throw new ServiceException("404","错误");
        }
        return new LoginUser(u,userMapper.getRoles(u.getId()));
    }
}
